package com.boco.modules.fdoc.api;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.boco.common.constants.BusinessConstants;
import com.boco.common.json.BaseJsonVo;
import com.boco.common.utils.JsonUtils;
import com.boco.common.web.BaseController;
import com.boco.modules.fdoc.model.DoctorEntity;
import com.boco.modules.fdoc.model.HospitalEntity;
import com.boco.modules.fdoc.service.DocService;
import com.boco.modules.fdoc.service.DoctorCommentService;
import com.boco.modules.fdoc.vo.DoctorDetailVo;
import com.boco.modules.fdoc.vo.ScheduleVo;
import com.wordnik.swagger.annotations.ApiOperation;

/**
 * 医生 api
 * 
 * @author sufj
 *
 */
@RestController
@RequestMapping(value = "/doctor", produces = "text/json;charset=utf-8")
public class DoctorApi extends BaseController {
	
	@Resource
	DocService docService;
	@Resource
	DoctorCommentService doctorCommentService;
	
	
	@RequestMapping(value = "/getDoctorList", method = RequestMethod.GET)
	@ApiOperation(value = "获取所有医生", notes = "{'hospId':'医院id','deptId':'科室id'}", response = BaseJsonVo.class)
	public String getDoctorList(@RequestParam int hospId,@RequestParam int deptId){
		DoctorEntity entity = new DoctorEntity();
		entity.setHospId(hospId);
		entity.setDeptId(deptId);
		List<DoctorEntity> list = docService.getDoctorList(entity);
		return JsonUtils.getJson(BaseJsonVo.success(list));
	}
	

	@RequestMapping(value = "/getDoctorDetail", method = RequestMethod.GET)
	@ApiOperation(value = "获取医生详情", notes = "{'id':'id'}", response = BaseJsonVo.class)
	public String getDoctorDetail(@RequestParam int id){
		return JsonUtils.getJson(BaseJsonVo.success(docService.getDoctorDetail(id)));
	}
	
	@RequestMapping(value = "/getDoctorSingleComments", method = RequestMethod.GET)
	@ApiOperation(value = "获取最近的两条评论", notes = "{'id':'id','commentLevel':'评价等级:0为全部评价,1为好评,2为中评,3为差评'}", response = BaseJsonVo.class)
	public String getDoctorSingleComments(@RequestParam int id,@RequestParam String commentLevel){
		DoctorDetailVo vo = new DoctorDetailVo();
		vo.setId(id);
		if (BusinessConstants.ALL_EVALUATE_CODE.equals(commentLevel)) {
			vo.setCommentLevel(BusinessConstants.ALL_EVALUATE);
		}else if (BusinessConstants.GOOD_EVALUATE_CODE.equals(commentLevel)) {
			vo.setCommentLevel(BusinessConstants.GOOD_EVALUATE);
		}else if (BusinessConstants.GENERAL_EVALUATE_CODE.equals(commentLevel)) {
			vo.setCommentLevel(BusinessConstants.GENERAL_EVALUATE);
		}else {
			vo.setCommentLevel(BusinessConstants.BAD_EVALUATE);
		}
		return JsonUtils.getJson(BaseJsonVo.success(docService.getSingleDocComment(vo)));
	}
	
	@RequestMapping(value = "/getDoctorSchedual", method = RequestMethod.GET)
	@ApiOperation(value = "获取医生排班", notes = "'docId':'医生id';", response = BaseJsonVo.class)
	public String getDoctorSchedual(@RequestParam String docId,@RequestParam Long startDate,@RequestParam Long endDate){
		ScheduleVo vo = new ScheduleVo();
		vo.setDocId(Integer.valueOf(docId));
		vo.setBeginTime(new Date(startDate));
		vo.setEndTime(new Date(endDate));
		return JsonUtils.getJson(BaseJsonVo.success(docService.getDocSchedual(vo)));
	}
	
	@RequestMapping(value = "/getDoctorByUid", method = RequestMethod.GET)
	@ApiOperation(value = "获取当前登录医生信息（医生端）", notes = "'uid':'医生登录uid';", response = BaseJsonVo.class)
	public String getDoctorByUid(@RequestParam String uid){
		DoctorEntity entity = docService.getDocByDocUid(uid);
		return JsonUtils.getJson(BaseJsonVo.success(docService.getDoctorDetail(entity.getId())));
	}
	
	@RequestMapping(value = "/getDocByName", method = RequestMethod.GET)
	@ApiOperation(value = "通过姓名查询医生", notes = "{'name':'医生姓名'}", response = BaseJsonVo.class)
	public String getDocByName(@RequestParam String name){
		BaseJsonVo vo = new BaseJsonVo();
		/**
		 *  本地tomcat编码和服务器不一致
		String doc = null;
		try {
			doc = new String(name.getBytes("ISO8859-1"),System.getProperty("file.encoding"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		*/
		BeanUtils.copyProperties(docService.getDocByName(name), vo);
		return JsonUtils.getJson(BaseJsonVo.success(vo));
	}
	
	@RequestMapping(value = "/getDefaultDocs", method = RequestMethod.GET)
	@ApiOperation(value = "获取排名前二十的医生", notes = "{'city':'城市'}", response = BaseJsonVo.class)
	public String getDefaultDocs(@RequestParam String city){
		/**
		 本地tomcat编码和服务器不一致
		String doc = null;
		try {
			doc = new String(city.getBytes("ISO-8859-1"),"utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		* 
		 */
		return JsonUtils.getJson(BaseJsonVo.success(docService.getDefaultDocs(city)));
	}
	
	@RequestMapping(value = "/getDocsByCityAndDept", method = RequestMethod.GET)
	@ApiOperation(value = "通过城市、科室ID查找医生", notes = "{'city':'城市','deptId':'科室ID'}", response = BaseJsonVo.class)
	public String getDocsByCityAndDept(@RequestParam String city,@RequestParam int deptId){
		/**
		// 本地tomcat编码和服务器不一致
		String doc = null;
		try {
			doc = new String(city.getBytes("ISO-8859-1"),"utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		*/
		return JsonUtils.getJson(BaseJsonVo.success(docService.getDoctorsByCityAndDept(city, deptId)));
	}
}
